<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>积分管理</el-breadcrumb-item>
      <el-breadcrumb-item>积分列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入用户手机号或用户ID（模糊搜索）"
            v-model="queryInfo.query"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getData()"
            ></el-button> </el-input
        ></el-col>

        <el-col :span="4"
          ><el-button type="primary" @click="handleShowSignInConfig"
            >签到配置</el-button
          ></el-col
        >
      </el-row>

      <el-table :data="scoreList" border style="width: 100%">
        <el-table-column type="index"> </el-table-column>
        <el-table-column prop="user_id" label="用户id"> </el-table-column>
        <el-table-column prop="phone_number" label="手机号"> </el-table-column>
        <el-table-column prop="total_score" label="总积分"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!-- 详细信息 -->
            <el-tooltip
              :enterable="false"
              effect="dark"
              content="详细信息"
              placement="top"
            >
              <el-button
                type="primary"
                icon="el-icon-postcard"
                circle
                @click="showInfoDialog(scope.row.user_id)"
              ></el-button>
            </el-tooltip>

            <!-- 修改按钮 -->
            <el-tooltip
              :enterable="false"
              effect="dark"
              content="修改"
              placement="top"
            >
              <el-button
                type="primary"
                icon="el-icon-edit"
                circle
                @click="showEditDialog(scope.row.user_id)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[10, 20, 50]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 积分详细信息 -->
    <el-dialog
      @close="listDialogVisible = false"
      :title="'积分详细信息（' + currScoreInfoList.length + '）'"
      :visible.sync="listDialogVisible"
      width="90%"
    >
      <el-table
        :data="currScoreInfoList"
        border
        height="50vh"
        style="width: 100%; font-size: 20px"
      >
        <el-table-column prop="id" label="id"> </el-table-column>
        <el-table-column prop="order_id" label="订单id"> </el-table-column>
        <el-table-column prop="detail" label="积分描述"> </el-table-column>
        <el-table-column prop="change_value" label="积分变化">
          <template slot-scope="scope">
            <div
              :style="
                scope.row.change_value > 0 ? 'color:#F56C6C' : 'color:#67C23A'
              "
            >
              {{ scope.row.change_value }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="创建时间">
          <template slot-scope="scope">
            {{ scope.row.create_time | dateFormat }}
          </template>
        </el-table-column>
      </el-table>

      <!-- 底部按钮区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="listDialogVisible = false" type="primary"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <!-- 修改用户积分 -->
    <el-dialog
      @close="editDialogClosed"
      title="修改积分"
      :visible.sync="editDialogVisible"
      width="60%"
    >
      <!-- 内容主体区 -->
      <el-form
        :model="editForm"
        :rules="editFormRules"
        ref="editFormRef"
        label-width="100px"
      >
        <el-form-item label="用户ID" prop="user_id">
          <el-input v-model="editForm.user_id" disabled></el-input>
        </el-form-item>

        <el-form-item label="变化积分" prop="change_value">
          <el-input v-model="editForm.change_value" type="number"></el-input>
        </el-form-item>
        <el-form-item label="积分描述" prop="detail">
          <el-input v-model="editForm.detail"></el-input>
        </el-form-item>
        <el-form-item label="增加/减少" prop="is_add">
          <el-switch
            v-model="editForm.is_add"
            active-text="增加"
            inactive-text="减少"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <!-- 底部按钮区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editUserScore">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 签到配置信息 -->
    <el-dialog title="签到配置" :visible.sync="signDialogVisible" width="20%">
      <el-form
        :model="signConfigForm"
        :rules="signConfigFormRules"
        ref="signConfigFormRef"
        label-width="100px"
      >
        <el-form-item label="初始积分" prop="defaultScore">
          <el-input
            v-model="signConfigForm.defaultScore"
            type="number"
          ></el-input>
        </el-form-item>
        <el-form-item label="积分增量" prop="addScore">
          <el-input v-model="signConfigForm.addScore" type="number"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部按钮区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="signDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editSignConfig">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: '',
        pageNum: 1,
        pageSize: 10
      },
      scoreList: [],
      currScoreInfoList: [],
      total: 0,
      listDialogVisible: false,
      editForm: {
        user_id: 0,
        change_value: '',
        detail: '',
        is_add: true
      },
      editDialogVisible: false,
      editFormRules: {
        change_value: [
          {
            required: true,
            message: '请输入积分',
            trigger: 'blur'
          }
        ],
        detail: [
          {
            required: true,
            message: '请输入改变积分的描述',
            trigger: 'blur'
          }
        ]
      },
      signDialogVisible: false,
      signConfigForm: {
        defaultScore: '',
        addScore: ''
      },
      signConfigFormRules: {
        defaultScore: [
          {
            required: true,
            message: '请输入初始签到积分',
            trigger: 'blur'
          }
        ],
        addScore: [
          {
            required: true,
            message: '请输入签到积分增量',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.getData()
  },

  methods: {
    async getData() {
      const { data: res } = await this.$http.get('/getScoreList', {
        params: this.queryInfo
      })

      // console.log(res)
      if (res.status !== 200) {
        return this.$message.error('获取积分列表失败')
      }

      this.total = res.data.totalNum
      this.scoreList = res.data.data
    },

    async showInfoDialog(userId) {
      this.listDialogVisible = true
      const { data: res } = await this.$http.get('/getScoreInfoList/' + userId)
      if (res.status !== 200) {
        return this.$message.error('获取积分列表失败')
      }
      this.currScoreInfoList = res.data.list
    },

    showEditDialog(userId) {
      // console.log(userId)
      this.editForm.user_id = userId
      this.editDialogVisible = true
    },

    async editUserScore() {
      // console.log(this.editForm)
      this.$refs.editFormRef.validate(async (valid) => {
        if (!valid) return
        const { data: res } = await this.$http.post('/updateScore', {
          user_id: this.editForm.user_id,
          change_value: parseInt(this.editForm.change_value),
          detail: this.editForm.detail,
          is_add: this.editForm.is_add
        })
        if (res.status !== 200) {
          return this.$message.error(res.message)
        }
        this.$message.success(res.message)
        this.editDialogVisible = false
        this.getData()
      })
    },

    editDialogClosed() {
      this.editForm = {
        user_id: 0,
        change_value: 0,
        detail: '',
        is_add: true
      }
    },
    async handleShowSignInConfig() {
      this.signDialogVisible = true
      const { data: res } = await this.$http.get('/getSignInConfig')
      console.log(res)
      this.signConfigForm = {
        defaultScore: res.data.d_score,
        addScore: res.data.add_score
      }
    },

    editSignConfig() {
      this.$refs.signConfigFormRef.validate(async (valid) => {
        if (!valid) return

        const { data: res } = await this.$http.post('/setSignInConfig', {
          default_score: parseInt(this.signConfigForm.defaultScore),
          add_score: parseInt(this.signConfigForm.addScore)
        })
        if (res.status !== 200) {
          return this.$message.error(res.message)
        }
        this.$message.success(res.message)
        this.signDialogVisible = false
      })
    },
    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize
      this.getData()
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      this.queryInfo.pageNum = newPage
      this.getData()
    }
  }
}
</script>

<style lang="less" scoped></style>
